package it.orma.iridehr.views;

import it.orma.iridehr.entities.AltraConoscenzaInformatica;
import it.orma.iridehr.entities.AltraLingua;
import it.orma.iridehr.entities.AltreInformazioni;
import it.orma.iridehr.entities.ConoscenzeInformatiche;
import it.orma.iridehr.entities.DatiGenerali;
import it.orma.iridehr.entities.EsperienzaLavorativa;
import it.orma.iridehr.entities.LingueConosciute;
import it.orma.iridehr.entities.SchedaCandidato;
import it.orma.iridehr.entities.StudiCompiuti;
import it.orma.iridehr.entities.TitoloDiStudio;
import it.orma.iridehr.repository.RicercaRepository;
import it.orma.iridehr.services.helper.DocumentoAlfresco;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class CVPreviewShortView extends AbstractPdfView {

	
	String url;
	
	@Override
	protected void buildPdfDocument(Map<String, Object> model, Document doc,
			PdfWriter writer, HttpServletRequest request, HttpServletResponse arg4)
			throws Exception {
		doc.open();
		doc.setPageSize(PageSize.A4);
		DatiGenerali dg = (DatiGenerali) model.get("dg");
		List<EsperienzaLavorativa> esperienze = (List<EsperienzaLavorativa>) model.get("esperienzeLavorative");
		StudiCompiuti studi = (StudiCompiuti) model.get("studi");
		LingueConosciute lingue = (LingueConosciute)model.get("lingue");
		ConoscenzeInformatiche informatiche = (ConoscenzeInformatiche)model.get("informatica");
		AltreInformazioni aInfo = (AltreInformazioni)model.get("altro");
		
		InputStream logois = new FileInputStream(new File( (String) model.get("logo")));
		
		Paragraph paragraph = new Paragraph();		
		byte[] logob = new byte[logois.available()];
		logois.read(logob);
    	Image image = Image.getInstance(logob);
        image.setAlignment(Image.ALIGN_LEFT);
        paragraph.add(new Chunk("Test"));
        logois.close();

        BaseFont bf = createFont();
		PdfPTable table = new PdfPTable(new float[]{30f,2f,2f,66f});
		Phrase mainCV = new Phrase();
		mainCV.add(new Chunk(dg.getNome().substring(0,1).toUpperCase(),new Font(bf,13,Font.BOLD)));
		mainCV.add(new Chunk(dg.getNome().substring(1).toUpperCase(),new Font(bf,10,Font.BOLD)));
		mainCV.add(new Chunk(" "+dg.getCognome().substring(0,1).toUpperCase(),new Font(bf,13,Font.BOLD)));
		mainCV.add(new Chunk(dg.getCognome().substring(1).toUpperCase(),new Font(bf,10,Font.BOLD)));
		mainCV.add(new Chunk("\nC",new Font(bf,13,Font.BOLD)));
		mainCV.add(new Chunk("URRICULUM ",new Font(bf,10,Font.BOLD)));
		mainCV.add(new Chunk("V",new Font(bf,13,Font.BOLD)));
		mainCV.add(new Chunk("ITAE",new Font(bf,10,Font.BOLD)));
				
		PdfPCell leftCell = new PdfPCell(mainCV);
		leftCell.setBorder(0);
		
		PdfPCell centerCell1 = new PdfPCell(new Phrase(""));
		centerCell1.setBorder(Rectangle.RIGHT);
		PdfPCell centerCell2 = new PdfPCell(new Phrase(""));
		centerCell2.setBorder(0);
		
		PdfPCell rightCell ;
		
		if (dg.getFoto()!=null)
			rightCell = new PdfPCell(Image.getInstance(dg.getFoto()));
		else 
			rightCell = new PdfPCell(new Phrase(""));
		
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		Phrase personalInfo = new Phrase();
		personalInfo.add(new Chunk("I",new Font(bf,11,Font.BOLD)));
		personalInfo.add(new Chunk("NFORMAZIONI",new Font(bf,9,Font.BOLD)));
		personalInfo.add(new Chunk(" P".toUpperCase(),new Font(bf,11,Font.BOLD)));
		personalInfo.add(new Chunk("ERSONALI",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(personalInfo);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		
		leftCell = new PdfPCell(new Phrase("Cognome e Nome",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getCognome()+" "+dg.getNome(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Luogo e data di nascita",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getComuneDiNascita()+"("+dg.getProvinciaDiNascita()+"), "+new SimpleDateFormat("dd/MM/yyyy").format(dg.getDataDiNascita()),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Residenza",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getIndirizzoResidenza()+","+dg.getCapResidenza()+" "+dg.getComuneResidenza()+"("+dg.getProvinciaResidenza()+")",new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Nazionalità",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getNazionalita(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Sesso",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getSesso(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Stato civile",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getStatoCivile(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Telefono fisso",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getTelefonoCasa(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Telefono mobile",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(dg.getTelefonoCell1(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);	
		
		Phrase studiInfo = new Phrase();
		studiInfo.add(new Chunk("I",new Font(bf,11,Font.BOLD)));
		studiInfo.add(new Chunk("STRUZIONE E",new Font(bf,9,Font.BOLD)));
		studiInfo.add(new Chunk(" F".toUpperCase(),new Font(bf,11,Font.BOLD)));
		studiInfo.add(new Chunk("ORMAZIONE",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(studiInfo);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		if (studi!=null){
			Iterator<TitoloDiStudio> it = studi.getTitoliDiStudio().iterator();
			while (it.hasNext()){
				leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				TitoloDiStudio item = it.next();
				leftCell = new PdfPCell(new Phrase("Anno ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getAnno().toString(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				leftCell = new PdfPCell(new Phrase("Nome dell'istituto ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getIstituto(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				leftCell = new PdfPCell(new Phrase("Tipologia titolo",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getTipologiaTitoloDiStudio(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				leftCell = new PdfPCell(new Phrase("Votazione ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getVotazione(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				if (item.getTitoloTesi()!=null&&item.getTitoloTesi().length()>0){
					leftCell = new PdfPCell(new Phrase("Titolo Tesi ",new Font(bf,9,Font.NORMAL)));
					leftCell.setBorder(0);
					leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					rightCell = new PdfPCell(new Phrase(item.getTitoloTesi(),new Font(bf,9,Font.NORMAL)));
					rightCell.setBorder(0);
					rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
					table.addCell(leftCell);
					table.addCell(centerCell1);
					table.addCell(centerCell2);
					table.addCell(rightCell);
				}
			}
			
			leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
			leftCell.setBorder(0);
			rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
			rightCell.setBorder(0);
			table.addCell(leftCell);
			table.addCell(centerCell1);
			table.addCell(centerCell2);
			table.addCell(rightCell);
			
			leftCell = new PdfPCell(new Phrase("Altri corsi/stage ",new Font(bf,9,Font.NORMAL)));
			leftCell.setBorder(0);
			leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			rightCell = new PdfPCell(new Phrase(studi.getCorsiDiFormazione(),new Font(bf,9,Font.NORMAL)));
			rightCell.setBorder(0);
			rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(leftCell);
			table.addCell(centerCell1);
			table.addCell(centerCell2);
			table.addCell(rightCell);
		}
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		Phrase lingueInfo = new Phrase();
		lingueInfo.add(new Chunk("C",new Font(bf,11,Font.BOLD)));
		lingueInfo.add(new Chunk("OMPETENZE ",new Font(bf,9,Font.BOLD)));
		lingueInfo.add(new Chunk("L".toUpperCase(),new Font(bf,11,Font.BOLD)));
		lingueInfo.add(new Chunk("INGUISTICHE",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(lingueInfo);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		if (lingue!=null){
		
			Phrase madrelingua = new Phrase();
			madrelingua.add(new Chunk("MADRELINGUA",new Font(bf,10,Font.NORMAL)));	
			leftCell = new PdfPCell(madrelingua);
			leftCell.setBorder(0);
			leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			rightCell = new PdfPCell(new Phrase(lingue.getLinguaMadre1(),new Font(bf,10,Font.NORMAL)));
			rightCell.setBorder(0);
			table.addCell(leftCell);
			table.addCell(centerCell1);
			table.addCell(centerCell2);
			table.addCell(rightCell);
			
			leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
			leftCell.setBorder(0);
			rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
			rightCell.setBorder(0);
			table.addCell(leftCell);
			table.addCell(centerCell1);
			table.addCell(centerCell2);
			table.addCell(rightCell);
			
			Phrase altreLingue = new Phrase();
			altreLingue.add(new Chunk("ALTRE LINGUE",new Font(bf,10,Font.NORMAL)));	
			leftCell = new PdfPCell(altreLingue);
			leftCell.setBorder(0);
			leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			rightCell = new PdfPCell(new Phrase(lingue.getLinguaMadre1(),new Font(bf,10,Font.NORMAL)));
			rightCell.setBorder(0);
			table.addCell(leftCell);
			table.addCell(centerCell1);
			table.addCell(centerCell2);
			table.addCell(rightCell);
			
			if (lingue.getAltreLingue()!=null){
				Iterator<AltraLingua> it = lingue.getAltreLingue().iterator();
				String nextLingua = "";
				while (it.hasNext()){
					AltraLingua altraLingua = it.next();
					String livelloDecoded = "Base";
					if (altraLingua.getLivello().equals("2"))
						livelloDecoded = "Intermedio";
					else if (altraLingua.getLivello().equals("3"))
						livelloDecoded = "Avanzato";
					if (!altraLingua.getLingua().equals(nextLingua)){
					
						leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
						leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase(altraLingua.getLingua(),new Font(bf,11,Font.BOLD)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
						leftCell = new PdfPCell(new Phrase(altraLingua.getCapacita(),new Font(bf,9,Font.NORMAL)));
						leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase(livelloDecoded,new Font(bf,9,Font.NORMAL)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
						nextLingua = altraLingua.getLingua();
						
					}else{
						
						leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
						leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase("",new Font(bf,11,Font.BOLD)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
						leftCell = new PdfPCell(new Phrase(altraLingua.getCapacita(),new Font(bf,9,Font.NORMAL)));
						leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase(livelloDecoded,new Font(bf,9,Font.NORMAL)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
					}
					
					
				}
			}
			
			leftCell = new PdfPCell(new Phrase("Tempo speso all'estero ",new Font(bf,9,Font.NORMAL)));
			leftCell.setBorder(0);
			leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			rightCell = new PdfPCell(new Phrase(lingue.getEsperienzaEstero(),new Font(bf,9,Font.NORMAL)));
			rightCell.setBorder(0);
			rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(leftCell);
			table.addCell(centerCell1);
			table.addCell(centerCell2);
			table.addCell(rightCell);
			
		}
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		Phrase infoInfo = new Phrase();
		infoInfo.add(new Chunk("C",new Font(bf,11,Font.BOLD)));
		infoInfo.add(new Chunk("OMPETENZE ",new Font(bf,9,Font.BOLD)));
		infoInfo.add(new Chunk("T".toUpperCase(),new Font(bf,11,Font.BOLD)));
		infoInfo.add(new Chunk("ECNICHE",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(infoInfo);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		if (informatiche!=null){
			if (informatiche.getLivelloInternet()!=null){
				leftCell = new PdfPCell(new Phrase("Internet",new Font(bf,9,Font.NORMAL)));
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(informatiche.getLivelloInternet(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
			}
			if (informatiche.getLivelloDatabase()!=null){
				leftCell = new PdfPCell(new Phrase("Dabatase",new Font(bf,9,Font.NORMAL)));
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(informatiche.getLivelloDatabase(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
			}
			if (informatiche.getLivelloElaborazioneTesti()!=null){
				leftCell = new PdfPCell(new Phrase("Elaborazione Testi",new Font(bf,9,Font.NORMAL)));
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(informatiche.getLivelloElaborazioneTesti(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
			}
			if (informatiche.getLivelloFoglioCalcolo()!=null){
				leftCell = new PdfPCell(new Phrase("Foglio di Calcolo",new Font(bf,9,Font.NORMAL)));
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(informatiche.getLivelloFoglioCalcolo(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
			}
			if (informatiche.getAltreConoscenzeInformatiche()!=null){
				leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				Phrase altriProgrammi = new Phrase();
				altriProgrammi.add(new Chunk("ALTRI PROGRAMMI",new Font(bf,10,Font.NORMAL)));	
				leftCell = new PdfPCell(altriProgrammi);
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase("",new Font(bf,10,Font.NORMAL)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				Iterator<AltraConoscenzaInformatica> it = informatiche.getAltreConoscenzeInformatiche().iterator();
				String tipoProgramma = "";
				while (it.hasNext()){
					AltraConoscenzaInformatica altraConoscenza = it.next();
					
					if (!altraConoscenza.getTipo().equals(tipoProgramma)){
						String tipoDecoded = "Applicativo Gestionale";
						if (altraConoscenza.getTipo().equals("G")){
							tipoDecoded = "Programma di grafica";
						}else if (altraConoscenza.getTipo().equals("L")){
							tipoDecoded = "Linguaggio di Programmazione";
						}
						leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase(tipoDecoded,new Font(bf,11,Font.BOLD)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
						leftCell = new PdfPCell(new Phrase(altraConoscenza.getAltraConoscenza(),new Font(bf,9,Font.NORMAL)));
						leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase(altraConoscenza.getLivAltraConoscenza(),new Font(bf,9,Font.NORMAL)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
						tipoProgramma = altraConoscenza.getTipo();
						
					}else{
						
						leftCell = new PdfPCell(new Phrase(altraConoscenza.getAltraConoscenza(),new Font(bf,9,Font.NORMAL)));
						leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
						leftCell.setBorder(0);
						rightCell = new PdfPCell(new Phrase(altraConoscenza.getLivAltraConoscenza(),new Font(bf,9,Font.NORMAL)));
						rightCell.setBorder(0);
						table.addCell(leftCell);
						table.addCell(centerCell1);
						table.addCell(centerCell2);
						table.addCell(rightCell);
						
					}
					
					
				}
				
			}
			
		}
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		Phrase altreInfo = new Phrase();
		altreInfo.add(new Chunk("I",new Font(bf,11,Font.BOLD)));
		altreInfo.add(new Chunk("NFORMAZIONI",new Font(bf,9,Font.BOLD)));
		altreInfo.add(new Chunk(" V".toUpperCase(),new Font(bf,11,Font.BOLD)));
		altreInfo.add(new Chunk("ARIE",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(altreInfo);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		EsperienzaLavorativa attuale = null;
		if (esperienze!=null){
			Iterator<EsperienzaLavorativa> it = esperienze.iterator();
			while(it.hasNext()){
				EsperienzaLavorativa item = it.next();
				if (item.getIncorso()!=null&&item.getIncorso().booleanValue()==true)
					attuale = item;
				break;
			}
		}
		
		String statoAttuale;
		
		if(aInfo.getStatoAttuale()!=null)
			statoAttuale  = aInfo.getStatoAttuale();
		else{
			if (attuale!=null)
				statoAttuale = "occupato/a";
			else
				statoAttuale = "libero/a";
		}
			
		
		leftCell = new PdfPCell(new Phrase("Attualmente",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(statoAttuale,new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Attuale Occupazione",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		
		
		if (attuale!=null)		
			rightCell = new PdfPCell(new Phrase(attuale.getMansione()+" presso "+attuale.getPresso()+" di "+attuale.getComune()+"("+attuale.getProvincia()+")",new Font(bf,9,Font.NORMAL)));
		else
			rightCell = new PdfPCell(new Phrase("",new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);

		leftCell = new PdfPCell(new Phrase("Retribuzione Attuale",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		if (attuale!=null)		
			rightCell = new PdfPCell(new Phrase(attuale.getTipoRetribuzione()+" mensile di"+ attuale.getRetribuzioneMensile()+" per "+attuale.getMensilita()+" mensilità",new Font(bf,9,Font.NORMAL)));
		else
			rightCell = new PdfPCell(new Phrase("",new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Hobbies",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(aInfo.getHobbies(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		Phrase altreInfo2 = new Phrase();
		altreInfo2.add(new Chunk("P",new Font(bf,11,Font.BOLD)));
		altreInfo2.add(new Chunk("OSIZIONE",new Font(bf,9,Font.BOLD)));
		altreInfo2.add(new Chunk(" D".toUpperCase(),new Font(bf,11,Font.BOLD)));
		altreInfo2.add(new Chunk("ESIDERATA",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(altreInfo2);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Inquadramento Desiderato",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(nvl(aInfo.getInqDesiderato()),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Retribuzione Desiderata",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase("lorda mensile "+nvl(aInfo.getMensileLordo())+" per "+ nvl(""+aInfo.getNumeroMensilita()!=null?""+aInfo.getNumeroMensilita():"")+" mensilità",new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase("Preavviso",new Font(bf,9,Font.NORMAL)));
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(aInfo.getPreavviso(),new Font(bf,9,Font.NORMAL)));
		rightCell.setBorder(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		leftCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		leftCell.setBorder(0);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		Phrase esperienzeInfo = new Phrase();
		esperienzeInfo.add(new Chunk("E",new Font(bf,11,Font.BOLD)));
		esperienzeInfo.add(new Chunk("SPERIENZE",new Font(bf,9,Font.BOLD)));
		esperienzeInfo.add(new Chunk(" L".toUpperCase(),new Font(bf,11,Font.BOLD)));
		esperienzeInfo.add(new Chunk("AVORATIVE",new Font(bf,9,Font.BOLD)));		
		leftCell = new PdfPCell(esperienzeInfo);
		leftCell.setBorder(0);
		leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		rightCell = new PdfPCell(new Phrase(" ",new Font(bf,13,Font.BOLD)));
		rightCell.setBorder(0);
		table.addCell(leftCell);
		table.addCell(centerCell1);
		table.addCell(centerCell2);
		table.addCell(rightCell);
		
		if (esperienze!=null){
			Iterator<EsperienzaLavorativa> it = esperienze.iterator();
			while (it.hasNext()){
				leftCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
				leftCell.setBorder(0);
				rightCell = new PdfPCell(new Phrase(" ",new Font(bf,11,Font.BOLD)));
				rightCell.setBorder(0);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				EsperienzaLavorativa item = it.next();
				leftCell = new PdfPCell(new Phrase("Date (da -a) ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getDataDaStr()+" - "+item.getDataAStr(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				leftCell = new PdfPCell(new Phrase("Nome e indirizzo del datore di lavoro ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getPresso()+","+item.getProvincia(),new Font(bf,9,Font.NORMAL)));
				rightCell.setNoWrap(false);
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				leftCell = new PdfPCell(new Phrase("Settore ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getSettore(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
				leftCell = new PdfPCell(new Phrase("Mansione ",new Font(bf,9,Font.NORMAL)));
				leftCell.setBorder(0);
				leftCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				rightCell = new PdfPCell(new Phrase(item.getMansione(),new Font(bf,9,Font.NORMAL)));
				rightCell.setBorder(0);
				rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				table.addCell(leftCell);
				table.addCell(centerCell1);
				table.addCell(centerCell2);
				table.addCell(rightCell);
				
			}		
		}
		
		doc.add(table);
		
		doc.close();
		/*
		DocumentoAlfresco docAlf = (DocumentoAlfresco)model.get("docAlfresco");
		
		byte[] array = new byte[docAlf.getStream().available()];
		docAlf.getStream().read(array);
		PdfImportedPage page;
		PdfReader reader = new PdfReader(array);
        int numberOfPages = reader.getNumberOfPages();
       
        doc.open();
        for(int i = 0; i < numberOfPages; ){
            ++i;
            doc.open();
            PdfContentByte cb = writer.getDirectContent();

	        page = writer.getImportedPage(reader, i); 
	
	        // Copy first page of existing PDF into output PDF
	        doc.newPage();
	        cb.addTemplate(page, 0, 0);            
        }
        doc.close();
        writer.close();
        */
		
	}
	

	private BaseFont createFont() throws DocumentException, IOException{
		BaseFont bf= BaseFont.createFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		return bf;
	}

	@Override
	protected void buildPdfMetadata(Map<String, Object> model,
			Document document, HttpServletRequest request) {
		// TODO Auto-generated method stub
		super.buildPdfMetadata(model, document, request);
		
		InputStream logois;
		try {
			logois = new FileInputStream(new File( (String) model.get("logo")));
			
			Paragraph paragraph = new Paragraph();		
			
			byte[] logob = new byte[logois.available()];
			logois.read(logob);
	    	Image image = Image.getInstance(logob);
	        image.setAlignment(Image.ALIGN_LEFT);
	        paragraph.add(new Chunk(image, 0.0F, 0F));
	        logois.close();
	        
			HeaderFooter header = new HeaderFooter(paragraph,false);
			
			header.setBorder(0);
			document.setMargins(20, 20, 75, 40);
	        Boolean logoOn = (Boolean)model.get("logoOn");
	        if (logoOn!=null)
	        	document.setHeader(header);
	        
	        Paragraph paragraph2 = new Paragraph();		
	        paragraph2.add(new Chunk("                  Orma S.r.l. Via Enrico Fermi, 93 22030 Orsenigo(CO) - Tel.031.619.891 - Fax 031.33.50.058\n"+
	        		                 "                  www.orma.ws    -    email:     info@orma.ws    -   Cod.   Fisc.    e    Partita  IVA  02342930134",new Font(Font.HELVETICA,11,Font.NORMAL)));
	        paragraph2.setAlignment(Element.ALIGN_MIDDLE|Element.ALIGN_JUSTIFIED);
	        HeaderFooter footer = new HeaderFooter(paragraph2,false);
			document.setFooter(footer);
			footer.setBorder(0);
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadElementException e) {
			e.printStackTrace();
		}
		
	}
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public String nvl(String input){
		
		if (input==null)
			return "";
		return input;
	}
	

}
